package com.sokal.easycare.modules.contract.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.service.CrudService;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.contract.entity.ContractInfo;
import com.sokal.easycare.modules.contract.dao.ContractInfoDao;
import com.sokal.easycare.modules.contract.entity.ContractPaymentProportion;
import com.sokal.easycare.modules.contract.dao.ContractPaymentProportionDao;
import com.sokal.easycare.modules.contract.entity.ContractSchedule;
import com.sokal.easycare.modules.contract.dao.ContractScheduleDao;

/**
 * 合同信息Service
 * @author liliangdong
 * @version 2017-04-10
 */
@Service
@Transactional(readOnly = true)
public class ContractInfoService extends CrudService<ContractInfoDao, ContractInfo> {

	@Autowired
	private ContractPaymentProportionDao contractPaymentProportionDao;
	@Autowired
	private ContractScheduleDao contractScheduleDao;
	
	public ContractInfo get(String id) {
		ContractInfo contractInfo = super.get(id);
		contractInfo.setContractPaymentProportionList(contractPaymentProportionDao.findList(new ContractPaymentProportion(contractInfo)));
		contractInfo.setContractScheduleList(contractScheduleDao.findList(new ContractSchedule(contractInfo)));
		return contractInfo;
	}
	
	public List<ContractInfo> findList(ContractInfo contractInfo) {
		return super.findList(contractInfo);
	}
	
	public Page<ContractInfo> findPage(Page<ContractInfo> page, ContractInfo contractInfo) {
		return super.findPage(page, contractInfo);
	}
	
	@Transactional(readOnly = false)
	public void save(ContractInfo contractInfo) {
		super.save(contractInfo);
		for (ContractPaymentProportion contractPaymentProportion : contractInfo.getContractPaymentProportionList()){
			if (contractPaymentProportion.getId() == null){
				continue;
			}
			if (ContractPaymentProportion.DEL_FLAG_NORMAL.equals(contractPaymentProportion.getDelFlag())){
				if (StringUtils.isBlank(contractPaymentProportion.getId())){
					contractPaymentProportion.setContractInfo(contractInfo);
					contractPaymentProportion.preInsert();
					contractPaymentProportionDao.insert(contractPaymentProportion);
				}else{
					contractPaymentProportion.preUpdate();
					contractPaymentProportionDao.update(contractPaymentProportion);
				}
			}else{
				contractPaymentProportionDao.delete(contractPaymentProportion);
			}
		}
		for (ContractSchedule contractSchedule : contractInfo.getContractScheduleList()){
			if (contractSchedule.getId() == null){
				continue;
			}
			if (ContractSchedule.DEL_FLAG_NORMAL.equals(contractSchedule.getDelFlag())){
				if (StringUtils.isBlank(contractSchedule.getId())){
					contractSchedule.setContractInfo(contractInfo);
					contractSchedule.preInsert();
					contractScheduleDao.insert(contractSchedule);
				}else{
					contractSchedule.preUpdate();
					contractScheduleDao.update(contractSchedule);
				}
			}else{
				contractScheduleDao.delete(contractSchedule);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(ContractInfo contractInfo) {
		super.delete(contractInfo);
		contractPaymentProportionDao.delete(new ContractPaymentProportion(contractInfo));
		contractScheduleDao.delete(new ContractSchedule(contractInfo));
	}
	
}